const setHeartList = function(item) {

	let heartList = [];
	let heartList_Storage = uni.getStorageSync("heartList")
	if (heartList_Storage && JSON.parse(heartList_Storage).length > 0) {
		heartList = JSON.parse(heartList_Storage)
	}

	heartList.push(item);

	heartList = removeSame(heartList, "paper_id")
	uni.setStorageSync('heartList', JSON.stringify(heartList))
}

const cancelHeart = function(paper) {
	let heartList = [];
	let heartList_Storage = uni.getStorageSync("heartList")
	if (heartList_Storage && JSON.parse(heartList_Storage).length > 0) {
		heartList = JSON.parse(heartList_Storage)
	}
	heartList = heartList.filter(session => {
		return session.paper_id != paper.paper_id;
	})
	uni.setStorageSync('heartList', JSON.stringify(heartList))
}

const getHeartList = function() {
	let heartList = [];
	let heartList_Storage = uni.getStorageSync("heartList")
	if (heartList_Storage && JSON.parse(heartList_Storage).length > 0) {
		heartList = JSON.parse(heartList_Storage)
	}
	return heartList;
}

function removeSame(arr, prop) {
	let newArr = []; // 去重后得到的新数组
	const obj = {}; //  中间值 转换数据
	newArr = arr.reduce(function(pre, cur) {
		obj[cur[prop]] ? '' : (obj[cur[prop]] = true && pre.push(cur));
		return pre;
	}, []);
	return newArr;
}


export {
	setHeartList,
	getHeartList,
	cancelHeart
}